<template>
  <div class="box">
    <div id="tableBox">
      <el-button v-has @click="addVisible = true">新建任务</el-button>
      <el-button v-has @click="taskAlert">任务告警</el-button>
    </div>
    <el-table :data="tableData" border fit stripe highlight-current-row>
      <el-table-column v-for="item in tableHead" :key="item.id" :label="item.label" :prop="item.title" align="center">
        <template slot-scope="scope">
          {{ scope.row[item.title] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="scope">
          <el-button v-has size="mini" @click="(row = scope.row), (amendVisible = true)">任务操作</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="current-box">
      <el-pagination :current-page="currentPage" :page-sizes="[10, 30, 50, 100]" :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="totalCount" @size-change="handleSizeChange"
        @current-change="handleCurrentChange" />
    </div>
    <el-dialog title="新建任务" :close-on-click-modal="false" :visible.sync="addVisible" width="20%">
      <addTask @chang="close()" @getScheduPageList="getScheduPageList()" />
    </el-dialog>
    <template v-if="amendVisible">
      <el-dialog title="操作任务" :close-on-click-modal="false" :visible.sync="amendVisible" width="20%">
        <amendTask @chang="close()" :row="row" @getScheduPageList="getScheduPageList()" />
      </el-dialog>
    </template>
  </div>
</template>

<script>
  import addTask from "./components/add.vue";
  import amendTask from "./components/amend.vue";
  export default {
    components: {
      addTask,
      amendTask,
    },
    created() {
      this.getScheduPageList();
    },
    data() {
      return {
        tableHead: [{
            id: 1,
            label: "名称",
            title: "jobDesc",
          },
          {
            id: 2,
            label: "创建时间",
            title: "addTime",
          },
          {
            id: 3,
            label: "执行时间",
            title: "jobCron",
          },
          {
            id: 4,
            label: "负责人",
            title: "author",
          },
          {
            id: 5,
            label: "负责人",
            title: "alarmEmail",
          },
        ],
        tableData: [],
        currentPage: 1, // 当前页数
        totalCount: 100, // 数据总量
        pageSize: 10,
        addVisible: false,
        amendVisible: false,
        row: {},
      };
    },
    methods: {
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
        this.pageSize = val;
        this.getScheduPageList();
      },
      handleCurrentChange(val) {
        console.log(`当前页: ${val}`);
        this.currentPage = val;
        this.getScheduPageList();
      },
      getScheduPageList() {
        this.$server
          .getScheduPageList({
            page: this.currentPage,
            limit: this.pageSize
          })
          .then((res) => {
            console.log(res);
            this.tableData = res.page.list;
            this.totalCount = res.page.totalCount;
          });
      },
      close() {
        this.addVisible = false;
        this.amendVisible = false;
      },
      taskAlert() {
        this.$message({
          message: '已经向系统发送报警信息',
          type: 'error'
        })
      }
    },
  };
</script>

<style lang="scss" scoped>
  .box {
    padding: 20px;
  }

  #tableBox {
    margin-bottom: 20px;
  }

  .current-box {
    width: 100%;
    text-align: center;
    margin-top: 30px;
    margin-bottom: 30px;
  }
</style>
